package com.demo8.common.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.demo8.basic.domain.CommonResult;
import com.demo8.common.domain.CaptchaBodyVO;
import com.demo8.common.service.ICaptchaStrategyService;
import com.demo8.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 验证码接口
 */
@RestController
@RequestMapping("system")
public class CaptchaController {

    @Autowired
    Map<String, ICaptchaStrategyService> iCaptchaStrategyService;

    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 验证码
     *
     * @return
     */
    @SaIgnore
    @GetMapping(value = {"captcha/{type}/{target}",
            "captcha/{type}"})
    public CommonResult<?> getCaptcha(@PathVariable String type,
                                      @PathVariable(required = false) String target) {
        //判断是否启用验证码，不启动用直接返回
        if (!sysConfigService.selectCaptchaEnabled()) {
            CommonResult.ok(new CaptchaBodyVO(false));
        }
        return CommonResult.ok(iCaptchaStrategyService.get(type).generateCaptcha(target));
    }


}
